<?php
$title="Modify User Information";
$css="modifyuser.css";
include_once 'common/AdminTop.php';
include_once 'authentic.php';
include_once '../Class/DB.php';
include_once '../Class/User.php';
$db=new DB();
$uid=$_GET["id"];
$page=$_GET["page"];
$abc=$db->get_uid_info($uid);
$user=$abc[0];
$error_id=0;
	
	$userName=$_POST["name"];
if(!isset($userName))
{
	$userName=$user["userName"];	
}
	
	$sex=$_POST["sex"];
if(!isset($sex))
{
	$sex=$user["sex"];
}
	
	//$level=$_POST["level"];
//if(!isset($level)||$level=="")
//{
//	$level=$user["level"];
//}

	$exp=$_POST["exp"];
if(!isset($exp)||$exp=="")
{
	$exp=$user["exp"];
}

	$phone=$_POST["phone"];
if(!isset($phone))
{
	$phone=$user["phone"];
}

	$email=$_POST["email"];
if(!isset($email))
{
	$email=$user["email"];
}

	$address=$_POST["address"];
if(!isset($address))
{
	$address=$user["deliver_addr"];
}

if(($phone=="")||strlen($phone)!=11)
{
	$error_id=6;
}
 if (ereg("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/",$email)){
 	$error_id=5;
}
if($email=="")
{
	$error_id=5;
}
if($userName!="")
{
if($userName!=$user["userName"]&&$db->Is_exist($userName))
{
	$error_id=2;
}
}
if($userName==""){
	$error_id=1;
}

if (!isset($_POST["name"])){
	$error_id=-999;
}
if($error_id==0)
{
	$userTemp=new User();
	$userTemp->load_info($user["userName"]);
	$userTemp->update_userName($userName);
	$userTemp->level=$user["level"];
	$userTemp->exp=$exp;
	$userTemp->phone=$phone;
	$userTemp->deliver_addr=$address;
	$userTemp->sex=$sex;
	$userTemp->email=$email;
	$userTemp->update_self();
	header("Location:userList.php?page=$page");
}

?>
<div class="block_context" id="modify_block" align="center">
				<form action="modifyuser.php?page=<?php echo $page;?>&id=<?php echo $user["uid"];?>" method="post">
					<table>
						<tr>
							<th class="t1">
							</th>
							<th class="t2">
							</th>
							<th class="t3">
							</th>
						</tr>
						<tr>
							<td>
							<b>UserName:</b>
							</td>
							<td><input type="text" name="name" value="<?php echo $userName;?>" autocomplete="off" /></td>
							<?php if($error_id==1){?>
							<td style="color:red;">
								<p>Invalid UserName!</p>
							</td>
							<?php 	}?>
							<?php if($error_id==2){?>
							<td style="color:red;">
								<p>User already existed!</p>
							</td>
							<?php 	}?>
							
						</tr>
							<tr>
							<td>
							<b>points:</b>
							</td>
							<td><input type="text" name="exp" value="<?php echo $exp;?>" autocomplete="off" /></td>
							
							
						</tr>
						<tr>
							<td>
							<b>sex:</b>
							</td>
							<td>
								<input type="radio" name="sex" value="0" <?php if($sex==0){echo 'checked="checked"';}?>/>
								<label>male</label>
								<input type="radio" name="sex" value="1" <?php if($sex==1){echo 'checked="checked"';}?>/>
								<label>female</label>
							</td>
						</tr>
						<tr>
							<td>
							<b>Email:</b>
							</td>
							<td><input type="text" name="email" value="<?php echo $email;?>"/></td>
								<?php if($error_id==5){?>
							<td style="color:red;">
								<p>Invalid Email!</p>
							</td>
							<?php 	}?>
						</tr>
						<tr>
							<td>
							<b>Phone:</b>
							</td>
							<td><input type="text" name="phone" value="<?php echo $phone;?>"/></td>
								<?php if($error_id==6){?>
							<td style="color:red;">
							<p>Invalid phone!</p>
							</td>
							<?php 	}?>
						</tr>
						<tr>
							<td>
							<b>Address:</b>
							</td>
							<td><input type="text" name="address" value="<?php echo $address;?>"/></td>
						</tr>
						<tr>
							<td></td>
							<td>
							<p><input type="submit" value="Save" />
							<input type="button" value="Back" onclick="window.location.href='userList.php?page=<?php echo $page;?>'"/>							
							</p>
							</td>
							<td></td>	
						</tr>
					</table>
				</form>
			</div>
<?php 
include_once 'common/AdminTail.php';
?>